package com.yqb.study.modular.controller.data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yqb.study.common.controller.BaseController;
import com.yqb.study.common.rest.RestClient;
import com.yqb.study.common.vo.ResultVo;
import com.yqb.study.constant.CodeMsgEnum;
import com.yqb.study.modular.persistence.model.City;
import com.yqb.study.modular.service.ICityService;
import com.yqb.study.util.ResultVoUtil;
import com.yqb.study.util.ToolUtil;

import lombok.extern.slf4j.Slf4j;

// RestController返回json报文
@RestController
@Slf4j
@RequestMapping("/data")
public class RestFulController extends BaseController {
	
	@Autowired
	private RestClient restClient;
	
	@Autowired
	private ICityService cityService;
	
	// Restful服务对应的url地址  
    @Value("${springbootstudy.rest-service-path}")  
    private String restServicePath;
	
	// 通过该方法调用测试调用其他restful接口
	@GetMapping("/testRest")
	private Object testRest() {
		
		// 1.地址请求不到
		ResultVo<List<City>> resultVo = restClient.request(restServicePath + "/restGetNoPara");
		if (ToolUtil.isEmpty(resultVo)) {
			log.info("1-----客户端请求目的地址失败");
		}
		
		// 2.get请求无参数
		resultVo = restClient.requestGet(restServicePath + "/data/restGetNoPara");
		if (ToolUtil.isNotEmpty(resultVo)) {
			log.info("2-----客户端请求目的地址成功");
			
			log.info(resultVo.toString());
		}		
		
		// 3.get请求有参数
		Map<String, Object> uriVariables = new HashMap<>();
		uriVariables.put("id", 2);
		resultVo = restClient.requestGet(restServicePath + "/data/restGetWithPara", uriVariables);
		if (ToolUtil.isNotEmpty(resultVo)) {
			log.info("3-----客户端请求目的地址成功");
			
			log.info(resultVo.toString());
		}		
		
		// 4.post请求无参数
		resultVo = restClient.requestPost(restServicePath + "/data/restPostNoPara");
		if (ToolUtil.isNotEmpty(resultVo)) {
			log.info("4-----客户端请求目的地址成功");
			
			log.info(resultVo.toString());
		}		
		
		// 5.post请求有参数		
		uriVariables = new HashMap<>();
		uriVariables.put("id", 2);
		resultVo = restClient.requestPost(restServicePath + "/data/restPostWithPara", uriVariables);
		if (ToolUtil.isNotEmpty(resultVo)) {
			log.info("5-----客户端请求目的地址成功");
			
			log.info(resultVo.toString());
		}
		
		return ResultVoUtil.success();
	}
	
	
	@GetMapping("/restGetNoPara")
	public Object restGetNoPara() {
		
		List<City> cityList = cityService.getAllCityInfo();
		
		return ResultVoUtil.success(CodeMsgEnum.SERVER_SUCCESS, cityList);
	}	
	
	@GetMapping("/restGetWithPara")
	public Object restGetWithPara(@RequestParam(required = true) Integer id) {
		
		List<City> cityList = cityService.getCityInfoWith(id);
		
		return ResultVoUtil.success(CodeMsgEnum.SERVER_SUCCESS, cityList);
	}	
	
	@PostMapping("/restPostNoPara")
	public Object restPostNoPara() {
		
		List<City> cityList = cityService.getAllCityInfo();
		
		return ResultVoUtil.success(CodeMsgEnum.SERVER_SUCCESS, cityList);
	}	
	
	@PostMapping("/restPostWithPara")
	public Object restPostWithPara(@RequestParam(required = true) Integer id) {
		
		List<City> cityList = cityService.getCityInfoWith(id);
		
		return ResultVoUtil.success(CodeMsgEnum.SERVER_SUCCESS, cityList);
	}		
}
